﻿Public Class Form1
    Public conn As New SqlClient.SqlConnection()
    Public cmd As New SqlClient.SqlCommand()
    Public conn2 As New SqlClient.SqlConnection()
    Public cmd2 As New SqlClient.SqlCommand()
    Private Sub TabPage2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub
    Function refreshdata()
        Me.NewFileNamesTableAdapter.Fill(Me.FilesDBDataSet.NewFileNames)
        Me.FileNamesTableAdapter.Fill(Me.FilesDBDataSet.FileNames)
        Return 0
    End Function
    Function getfiles()

        conn.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\FilesDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
        conn2.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\FilesDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"

        cmd.Connection = conn
        cmd.CommandType = CommandType.Text
        conn.Open()
        cmd2.Connection = conn2
        cmd2.CommandType = CommandType.Text
        conn2.Open()


        If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
            del()
            Dim files
            Dim di As New IO.DirectoryInfo(FolderBrowserDialog1.SelectedPath)
            files = Dir(FolderBrowserDialog1.SelectedPath & "\*.*")

            Dir()
            RunReading(files, di)
            'Do
            'files = Dir(FolderBrowserDialog1.SelectedPath & "\*.", vbArchive)
            'RunReading(files, di)
            'files = Dir()
            ' Loop While Dir() <> ""
        End If
        DataGridView1.Refresh()
        conn.Close()
        conn2.Close()
        refreshdata()
        Return 0
    End Function
    Function RunReading(ByVal file, ByVal di)
        Dim n As Integer
        Dim k As Integer
        Dim onlyname As String
        Dim ext As String
        ' n = InStrRev(file.ToString, ".", Len(file.ToString), 1)
        ext = Microsoft.VisualBasic.Right(file.ToString, n)
        TextBox1.Text = ext
        While file <> ""
            n = Len(file.ToString)
            k = InStrRev(file.ToString, ".", n, 0)
            If k = 0 Then
                ext = ""
                onlyname = file.ToString
            Else
                ext = Microsoft.VisualBasic.Right(file.ToString, n - k)
                onlyname = Microsoft.VisualBasic.Left(file.ToString, k - 1)
            End If
            cmd.CommandText = "insert into FileNames (FileName,Path,OnlyName,Ext) values ('" & file.ToString & "','" & di.ToString & "','" & onlyname & "','" & ext & "')  "
            cmd2.CommandText = "insert into NewFileNames (FileName,Path,OnlyName,Ext) values ('" & file.ToString & "','" & di.ToString & "','" & onlyname & "','" & ext & "')  "
            file = Dir()
            cmd.ExecuteNonQuery()
            cmd2.ExecuteNonQuery()
        End While
        Return 0
    End Function
    Function del()
        Dim dconn As New SqlClient.SqlConnection()
        dconn.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\FilesDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
        Dim dcmd As New SqlClient.SqlCommand()
        dcmd.Connection = dconn
        dcmd.CommandType = CommandType.Text
        dconn.Open()
        dcmd.CommandText = "delete from FileNames where id>'0'"
        dcmd.ExecuteNonQuery()
        dconn.Close()
        refreshdata()
        Dim dconn2 As New SqlClient.SqlConnection()
        dconn2.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\FilesDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
        Dim dcmd2 As New SqlClient.SqlCommand()
        dcmd2.Connection = dconn2
        dcmd2.CommandType = CommandType.Text
        dconn2.Open()
        dcmd2.CommandText = "delete from NewFileNames where id>'0'"
        dcmd2.ExecuteNonQuery()
        dconn2.Close()
        Return 0
    End Function


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       
        refreshdata()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        getfiles()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        refreshdata()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        del()
    End Sub

    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

        If CheckBox20.Checked() Then
            DataGridView2.CurrentRow.Cells("FileName").Value = TextBox24.Text
        End If







    End Sub

    Private Sub FolderBrowserDialog1_HelpRequest(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FolderBrowserDialog1.HelpRequest

    End Sub
End Class
